package cn.rkylin.oms.goodscost.service;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageInfo;

import cn.rkylin.core.service.ApolloService;
import cn.rkylin.oms.common.consts.YesNoConst;
import cn.rkylin.oms.common.context.CurrentUser;
import cn.rkylin.oms.common.context.WebContextFactory;
import cn.rkylin.oms.goodscost.dao.IGoodsCostDAO;
import cn.rkylin.oms.goodscost.vo.GoodsCostVO;

/**
 * 
 * 类名：GoodsCostServiceImpl <br>
 * 作用：商品成本服务层 <br>
 * 创建时间：2018年1月16日 上午11:08:11
 *
 * @author LiPeng
 * @version
 */
@Service
public class GoodsCostServiceImpl extends ApolloService implements IGoodsCostService {

    /**
     * IGoodsCostDAO：售后事件数据层
     */
    @Autowired
    private IGoodsCostDAO goodsCostDAO;

    /**
     * queryGoodsCostList：查询商品成品列表
     */
    @Override
    public PageInfo<GoodsCostVO> queryGoodsCostList(int page, int length, GoodsCostVO queryParamVO) throws Exception {
        return goodsCostDAO.queryGoodsCostList(page, length, queryParamVO);
    }

    /**
     * getCostChangeList:查询商品成本变更列表
     */
    @Override
    public List<GoodsCostVO> getCostChangeList(GoodsCostVO queryParamVO) throws Exception {
        return goodsCostDAO.getCostChangeList(queryParamVO);
    }

    /**
     * updateGoodsCost：修改商品成本
     */
    @Override
    public String updateGoodsCost(GoodsCostVO goodsCostVO) throws Exception {
        String mString = "";
        // 修改商品成本
        GoodsCostVO updatePram = new GoodsCostVO();
        updatePram.setSkuId(goodsCostVO.getSkuId());
        updatePram.setCostPrice(goodsCostVO.getCostPrice() != null ? goodsCostVO.getCostPrice() : goodsCostVO.getCurtCost());
        updatePram.setRemark(goodsCostVO.getRemark());
        updatePram.setUpdateTime(new Date());
        int updateGoodsCost = goodsCostDAO.updateGoodsCost(updatePram);

        // 根据skuId查询商品
        GoodsCostVO preparePram = queryGoodsCostById(goodsCostVO.getSkuId());

        // 新增商品成本变更
        GoodsCostVO insertPram = getgoodsCostVO(goodsCostVO, preparePram);
        int insertGoodsCostChanger = goodsCostDAO.insertGoodsCostChanger(insertPram);

        if ((updateGoodsCost > 0) && (insertGoodsCostChanger > 0)) {
            mString = "修改成功";
        }
        return mString;
    }

    /**
     * getgoodsCostVO:为保存成本变更准备数据
     * 
     * @param goodsCostVO
     * @return
     * @throws Exception
     */
    private GoodsCostVO getgoodsCostVO(GoodsCostVO goodsCostVO, GoodsCostVO preparePram) throws Exception {

        // 创建时间
        if (StringUtils.isEmpty(goodsCostVO.getGoodsCostId())) {
            goodsCostVO.setCreateTime(new Date());
        }
        // 修改时间
        goodsCostVO.setUpdateTime(new Date());
        // id
        if (StringUtils.isEmpty(goodsCostVO.getGoodsCostId())) {
            goodsCostVO.setGoodsCostId(java.util.UUID.randomUUID().toString().replaceAll("-", ""));
        }
        // 获取当前登录人
        CurrentUser currentUser = WebContextFactory.getWebContext().getCurrentUser();
        // 企业Id
        if (StringUtils.isEmpty(goodsCostVO.getEntId())) {
            goodsCostVO.setEntId(preparePram.getEntId());
        }
        // 操作人姓名
        if (StringUtils.isEmpty(goodsCostVO.getOperUserName())) {
            goodsCostVO.setOperUserName(currentUser.getFullName());
        }
        // 规格id
        if (StringUtils.isEmpty(goodsCostVO.getSkuId())) {
            goodsCostVO.setSkuId(preparePram.getSkuId());
        }
        // 规格商家编码
        if (StringUtils.isEmpty(goodsCostVO.getSkuCode())) {
            goodsCostVO.setSkuCode(preparePram.getSkuCode());
        }
        // 规格名称
        if (StringUtils.isEmpty(goodsCostVO.getSkuName())) {
            goodsCostVO.setSkuName(preparePram.getSkuName());
        }
        // 商品id
        if (StringUtils.isEmpty(goodsCostVO.getGoodsId())) {
            goodsCostVO.setGoodsId(preparePram.getGoodsId());
        }
        // 商品商家编码
        if (StringUtils.isEmpty(goodsCostVO.getGoodsCode())) {
            goodsCostVO.setGoodsCode(preparePram.getGoodsCode());
        }
        // 商品名称
        if (StringUtils.isEmpty(goodsCostVO.getGoodsName())) {
            goodsCostVO.setGoodsName(preparePram.getGoodsName());
        }
        // 原成本价
        if (StringUtils.isEmpty(goodsCostVO.getOrigCost().toString())) {
            goodsCostVO.setOrigCost(preparePram.getCostPrice());
        }
        // 当前成本价
        // 操作时间

        // 操作类型:采购入库、退货入库、其他
        if (StringUtils.isEmpty(goodsCostVO.getOperType())) {
            goodsCostVO.setOperType("手动变更");
        }
        // 操作模式:手动、自动
        // 来源单据ID
        // if (StringUtils.isNotEmpty(goodsCostVO.getSourceId())) {
        // goodsCostVO.setSourceId(goodsCostVO.getSourceId());
        // }
        // 来源单据编码
        // if (StringUtils.isNotEmpty(goodsCostVO.getSourceCode())) {
        // goodsCostVO.setSourceCode(goodsCostVO.getSourceCode());
        // }
        // 备注
        // 删除标记
        goodsCostVO.setDeleted(YesNoConst.NO);
        return goodsCostVO;
    }

    /**
     * queryGoodsCostById：获取商品成本
     */
    @Override
    public GoodsCostVO queryGoodsCostById(String skuId) throws Exception {

        GoodsCostVO parm = new GoodsCostVO();
        parm.setSkuId(skuId);
        CurrentUser currentUser = WebContextFactory.getWebContext().getCurrentUser();
        String projectId = currentUser.getCurrentProject().getPrjId();
        parm.setPrjId(projectId);
        List<GoodsCostVO> list = goodsCostDAO.queryGoodsCostById(parm);

        GoodsCostVO returnObject = new GoodsCostVO();
        if (list.size() > 0) {
            returnObject = list.get(0);
        }
        return returnObject;
    }

}
